"""用来给用户提供录制动作的方法"""
import cv2
import mediapipe as mp
import Pose.Dynamic_Data_Struct
import Dynamic_Data,Dynamic_Tools,Dynamic_Data_Struct
#from Pose.Dynamic_Data_Struct import *


from Pose import Dynamic_Data_Struct
#from bank.Pose.文件 import 类
from Pose.Dynamic_Data_Struct import landmark_2d
import time

import pickle

def Record():
    """返回值。动作序列"""
    Action_List=[]

    """采集倒计时"""
    COUNT_DOWN=5

    cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)  # 摄像头
    mp_Pose = mp.solutions.pose
    pose = mp_Pose.Pose()  # 人体
    mpHands = mp.solutions.hands
    hands = mpHands.Hands()  # 手
    mpDraw = mp.solutions.drawing_utils
    mpDraw_styles = mp.solutions.drawing_styles
    mp_holistic = mp.solutions.holistic

    """倒计时"""
    for i in range(COUNT_DOWN):
        time.sleep(1)
        print(COUNT_DOWN - i)

    """开始录制"""
    for i in range(Dynamic_Data.WINDOW_SIZE):#录连续15帧

        ret, img = cap.read()  # 一次循环截一张图
        if ret:
            imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)  # imgRGB是一张图片，cv拿到的图是bgr的，转成rgb
            result_pose = pose.process(imgRGB)  # 把这个图片给hands对象处理,result是侦测出来的手
            result_hand = hands.process(imgRGB)

            if result_pose.pose_landmarks:

                """如果采集到人体或手部"""
                lm_1shot = Dynamic_Tools.Get_All_Landmark(result_pose.pose_landmarks)  # 不在摄像头内的部位会自己补全
                lm_1shot=Dynamic_Tools.transform_std(lm_1shot)#坐标变换和远近标准化
                Action_List.append(lm_1shot)
                print("第", i, "帧已经录完")

                """画图"""
                mpDraw.draw_landmarks(
                    img,
                    result_pose.pose_landmarks,
                    mp_holistic.POSE_CONNECTIONS,
                    landmark_drawing_spec=mpDraw_styles
                        .get_default_pose_landmarks_style())


            cv2.namedWindow('show')
            # cv2.resizeWindow("show", 840, 680);
            cv2.imshow('show', img)

        if cv2.waitKey(1) == ord('q'):  # 按q结束摄像头
            break

    if len(Action_List)==Dynamic_Data.WINDOW_SIZE-1:
        print("录制成功")
        """写入文件"""
        file = open('F:\\project\\Gesture_rec\\Data_Base\\Action4.txt', 'wb')
        pickle.dump(Action_List, file)
        file.close()
    else:
        print("录制失败，帧数=",len(Action_List))

def Read():
    """将动作文件读回"""
    file = open('F:\\project\\Gesture_rec\\Data_Base\\Action1.txt', 'rb')
    import sys
    # sys.path.append('F:\project\Gesture_rec\Pose\Dynamic_Data_Struct\landmark_2d')
    Action_List1=pickle.load(file)
    file.close()

    file = open('F:\\project\\Gesture_rec\\Data_Base\\Action2.txt', 'rb')
    Action_List2 = pickle.load(file)
    file.close()

    file = open('F:\\project\\Gesture_rec\\Data_Base\\Action3.txt', 'rb')
    Action_List3 = pickle.load(file)
    file.close()

    file = open('F:\\project\\Gesture_rec\\Data_Base\\Action4.txt', 'rb')
    Action_List4 = pickle.load(file)
    file.close()

    file = open('F:\\project\\Gesture_rec\\Data_Base\\Action5.txt', 'rb')
    Action_List5 = pickle.load(file)
    file.close()

    file = open('F:\\project\\Gesture_rec\\Data_Base\\Action6.txt', 'rb')
    Action_List6 = pickle.load(file)
    file.close()

    file = open('F:\\project\\Gesture_rec\\Data_Base\\Action7.txt', 'rb')
    Action_List7 = pickle.load(file)
    file.close()

    file = open('F:\\project\\Gesture_rec\\Data_Base\\Action8.txt', 'rb')
    Action_List8 = pickle.load(file)
    file.close()
    return Action_List1,Action_List2,Action_List3,Action_List4,Action_List5,Action_List6,Action_List7,Action_List8

def Connect2Data():
    """加载手势文件,之后再慢慢丰满功能"""
    return Read()

#if __name__ == '__main__':
    #1：录制
    #Record()

    #2：读取
    # print(Read())
    # a=Read()
    # a=1